package com.companyName.generator;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;

public class CodeGenerator {
	private static String REPLACE_FILE_CONTEXT_PATTERN = "^(import|package|.*companyName.*|<%.*import.*=|.*com.*dao.*|\\s*<.*>fw\\-.*<.*>).*";

	private static String SAMPLE_PLACEHOLDER = "sample|fw-sample";
	private static String COMPANY_PLACEHOLDER = "companyName";//"company-name";

	public void generate(String sampleDir, String targetDir, String rojectName,String companyName){
		System.out.println("=============自动生成开始===============");
		File sampleDirFile = new File(sampleDir);
		if(!sampleDirFile.exists() || !sampleDirFile.isDirectory()){
			System.err.println(sampleDir + " is not a dirctory");
			return;
		}
		
		File targetDirFile = new File(targetDir);
		if(targetDirFile.exists()){
			System.out.println("clean sub files of " + targetDir);
			File [] tdrs = targetDirFile.listFiles();
			for(File tdr : tdrs){
				if(tdr.isFile()){
					tdr.delete();
				}else{
					delete(tdr);
				}
			}
		}else{
			targetDirFile.mkdirs();
		}
		
		
		File [] files = sampleDirFile.listFiles(new SampleFileFilter());
		
		for(File f : files){
			scanFile(f, targetDir, rojectName,companyName);
		}
		System.out.println("=============自动生成完成===============");
	}
	
	public static void scanFile(File f, String targetDir, String rojectName, String companyName){
	    if(f.isDirectory()){
	    	String dirName = f.getName();
	    	dirName = replacePathPlaceholder(dirName, rojectName,companyName);
	    	File newDir = new File(targetDir, dirName);
	    	newDir.mkdirs();
	    	
	    	File [] files = f.listFiles(new SampleFileFilter());
	    	for(File f2 : files){
				scanFile(f2, targetDir + File.separator + dirName, rojectName,companyName);
	    	}
	    } else {
	    	String fName = f.getName();
	    	File newFile = new File(targetDir, fName);
	    	try {
				newFile.createNewFile();
				replaceFilePlaceholder(f, newFile, rojectName,companyName);
			} catch (Exception e) {
				System.err.println(e.getMessage());
				return;
			}
	    }
	}


	
	private static String replacePathPlaceholder(String dirName, String rojectName, String companyName) {
		dirName = dirName.replaceAll(COMPANY_PLACEHOLDER, companyName);//替换公司名称
		dirName = dirName.replaceAll(SAMPLE_PLACEHOLDER, rojectName);//替换项目名称
		return dirName;
	}


	private static void replaceFilePlaceholder(File sourceFile, File newFile, String rojectName, String companyName) {
			try (
					Scanner scanner = new Scanner(sourceFile);
					PrintWriter pr = new PrintWriter(new FileWriter(newFile));
			){
				while(scanner.hasNextLine()){
					String line = scanner.nextLine();
					if(line != null){
						if(line.matches(REPLACE_FILE_CONTEXT_PATTERN)){
							line = line.replaceAll(SAMPLE_PLACEHOLDER, rojectName);//替换项目名称
							line = line.replaceAll(COMPANY_PLACEHOLDER, companyName);//替换公司名称
						}
					}
					pr.println(line);
				}
			} catch (FileNotFoundException e) {
				System.err.println(sourceFile + " is not found");
				return;
			} catch (IOException e) {
				System.err.println("replace file placeholder error");
				return;
			}
		
	}

	private void delete(File f){
		if(f.isFile()){
			f.delete();
		}else{
			File [] fs = f.listFiles();
			for(File f2 : fs){
				delete(f2);
			}
			f.delete();
		}
	}
}


